<template>
  <div>
    <el-button @click="add_category" class="add">新增</el-button>
    <el-table :data="categoryList" style="width: 100%">
      <el-table-column prop="no" label="序号" align='center'>
      </el-table-column>
      <el-table-column prop="name" label="分类名称" align='center'>
      </el-table-column>
      <el-table-column prop="description" label="分类简介" align='center'>
      </el-table-column>
      <el-table-column label="操作" align='center'>
        <template v-slot="scope">
          <div>
            <el-button type="primary" @click="edit(scope.row)" size="small" icon="el-icon-edit"></el-button>
            <el-button type="danger" @click="delete_category(scope.row.id)" size="small" icon="el-icon-delete">
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div>
      <!-- 模态框 -->
      <el-dialog :title="title" :visible.sync="dialog_category" width="50%">
        <el-form :model="form_category">
          <el-form-item label="名称" :label-width="formLabelWidth_category">
            <el-input v-model="form_category.name"></el-input>
          </el-form-item>
          <el-form-item label="介绍" :label-width="formLabelWidth_category">
            <el-input v-model="form_category.description" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="序号" :label-width="formLabelWidth_category">
            <el-input v-model="form_category.no"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialog_category= false">取 消</el-button>
          <el-button type="primary" @click="save" class="add">确 定</el-button>
        </div>
      </el-dialog>
    </div>

  </div>
</template>
<script>
  import {
    mapActions,
    mapState
  } from 'vuex'
  export default {
    data() {
      return {
        dialog_category: false,
        formLabelWidth_category: "120px",
        form_category: {},
        title: "新增分类",

      }
    },
    computed: {
      ...mapState('category', ['categoryList'])
    },
    methods: {
      ...mapActions('category', ['findAll', 'saveOrUpdate', 'deleteById']),
      add_category() {
        this.title = '新增分类'
        this.dialog_category = true;
      },
      save() {
        this.saveOrUpdate(this.form_category).then(() => {
          this.dialog_category = false;
          this.findAll();
          this.form_category = {};
        })
      },
      edit(val) {
        console.log(val, '=========');
        // 0. 修改模态框标题
        this.title = "录入分类信息";
        // 1. 弹出模态框
        this.dialog_category = true;
        // 2. 将当前行数据放入模态框
        this.form_category = JSON.parse(JSON.stringify(val));
      },
      delete_category(id) {
        this.$confirm('此操作将永久删除该栏目, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var obj_delete = {
            id: id
          }
          this.deleteById(obj_delete).then(() => {
            this.$message({
              showClose: true,
              message: '删除成功！',
              type: 'success'
            });
            this.findAll()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
    },
    created() {
      this.findAll()
    }
  }

</script>
<style scoped>
  .add {
    color: #fff;
    background-color: #2cb5ac;
    border-color: #2cb5ac;
  }

</style>
